package com.rtf.framework.runconfig.impl;

import com.rtf.framework.common.util.UtilString;
import com.rtf.framework.runconfig.AppRunConfigService;
import com.rtf.framework.runconfig.config.AppRunConfigProperties;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Transactional(readOnly = true)
@Service
@Slf4j
public class AppRunConfigServiceImpl implements AppRunConfigService , InitializingBean {

    private static ScheduledExecutorService queryScheduler = null ;

    @Autowired
    private AppRunConfigDao appRunConfigDao ;

    @Autowired
    private AppRunConfigProperties appRunConfigProperties ;

    private volatile Map<String,String> configValues = Maps.newHashMap() ;

    protected String queryConfigValue(String key){
        if(UtilString.isBlank( key )){
            return null ;
        }

        key = key.toLowerCase().trim() ;
//
//        AppRunConfig appRunConfig = null ;
//
//        try{
//            appRunConfig = appRunConfigDao.getByKey( key ) ;
//        }catch( Exception e ){
//            log.error( "获取配置key: {} 失败 , 异常信息为: {}" , key , e.getMessage() ) ;
//        }
//
//        // 获取配置value，并将value转换为小写，并截取空字符
//        String value = appRunConfig!=null && UtilString.isNotBlank( appRunConfig.getValue() ) ?
//                appRunConfig.getValue().toLowerCase().trim() : null ;

        String value = configValues.get( key ) ;

        return value ;
    }

    @Override
    public boolean enable(String configKey) {
        String value = queryConfigValue( configKey ) ;

        return UtilString.equalsIgnoreCase( value , "true" ) ;
    }

    @Override
    public String getValue(String configKey) {
        return queryConfigValue( configKey ) ;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 创建固定大小的任务调度线程池
        if( queryScheduler==null ){
            queryScheduler = new ScheduledThreadPoolExecutor(2) ;
            queryScheduler.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    log.debug("查询apprunconfig开始");
                    try{
                        List<AppRunConfig> appRunConfigList = appRunConfigDao.findAll() ;
                        configValues.clear();
                        if( appRunConfigList!=null && appRunConfigList.size()>0 ){
                            for (AppRunConfig appRunConfig : appRunConfigList) {
                                configValues.put( appRunConfig.getKey().toLowerCase().trim() , appRunConfig.getValue() ) ;
                            }
                        }
                    }catch( Exception e ){
                        log.error( "查询apprunconfig异常 : {}" , e.getMessage() ) ;
                    }
                }
            } , 5 , appRunConfigProperties.getQueryPeriod() , TimeUnit.SECONDS ) ;
        }

    }



}
